<template>
    <el-dialog :title="!!id ? $t('update') : $t('add')" :visible.sync="open" width="500px" append-to-body>
        <el-form v-loading="loading" :model="form" ref="form" :rules="rules" label-width="100px">
            <el-form-item :label="$t('scada.gallery.309456-13')" prop="fileName">
                <el-input v-model="form.fileName" :placeholder="$t('scada.gallery.309456-14')" clearable style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item :label="$t('scada.gallery.309456-11')" prop="categoryName">
                <el-select v-model="form.categoryName" :placeholder="$t('scada.gallery.309456-12')" filterable clearable style="width: 300px">
                    <el-option v-for="dict in dict.type.scada_gallery_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSave">{{ $t('save') }}</el-button>
            <el-button @click="handleCancel">{{ $t('cancel') }}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { getGallery, updateGallery } from '@/api/scada/gallery';

export default {
    name: 'detail-dialog',
    dicts: ['scada_gallery_type'],
    props: {
        id: {
            type: Number,
            default: null,
        },
    },
    watch: {
        id: function (val) {
            if (val) {
                this.getDetailData();
            }
        },
    },
    data() {
        return {
            loading: false,
            open: false, // 是否显示
            // 表单校验
            rules: {
                categoryName: [
                    {
                        required: true,
                        message: this.$t('scada.gallery.309456-12'),
                        trigger: 'change',
                    },
                ],
                fileName: [
                    {
                        required: true,
                        message: this.$t('scada.gallery.309456-14'),
                        trigger: 'change',
                    },
                ],
            },
            // 表单参数
            form: {
                categoryName: '',
                fileName: '',
            },
        };
    },
    methods: {
        // 获取详情
        getDetailData() {
            this.loading = true;
            getGallery(this.id)
                .then((res) => {
                    if (res.code === 200) {
                        this.form = res.data;
                    }
                    this.loading = false;
                })
                .catch((err) => {
                    this.loading = false;
                });
        },
        // 保存数据
        handleSave() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.id) {
                        updateGallery(this.form).then((res) => {
                            if (res.code === 200) {
                                this.$modal.msgSuccess(this.$t('updateSuccess'));
                                this.open = false;
                                this.$emit('save');
                            } else {
                                this.$modal.msgError(res.msg);
                            }
                        });
                    }
                }
            });
        },
        // 取消
        handleCancel() {
            this.open = false;
        },
    },
};
</script>
